﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DatePickerRequied.ascx.cs" Inherits="PayrollHR.Administration.Modules.DatePickerRequied" %>
<asp:TextBox runat="server" MaxLength = "10" onkeyup = "ValidateDate(this, event.keyCode)" onkeydown = "return DateFormat(this, event.keyCode)" ID="txtDateTime" />
<asp:RequiredFieldValidator ID="rfvValue" ControlToValidate="txtDateTime" Font-Name="verdana"
    Font-Size="9pt" runat="server" Display="None"></asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="rfvValueE" TargetControlID="rfvValue"
    HighlightCssClass="validatorCalloutHighlight" />

<asp:ImageButton runat="Server" ID="btnCalendar" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="ShowCalendar" />
<br />
<asp:Label ID="lblMesg" runat="server" ForeColor = "Blue" Font-Names = "Arial" Text =""></asp:Label>
<ajaxToolkit:CalendarExtender runat="server" ID="ajaxCalendar" Format="dd/MM/yyyy" TargetControlID="txtDateTime" PopupButtonID="btnCalendar" /> 
<script type = "text/javascript">
    var isShift = false;
    var seperator = "/";
    function DateFormat(txt, keyCode) {
        if (keyCode == 16)
            isShift = true;
        //Validate that its Numeric
        if (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 ||
         keyCode <= 37 || keyCode <= 39 ||
         (keyCode >= 96 && keyCode <= 105)) && isShift == false) {
            if ((txt.value.length == 2 || txt.value.length == 5) && keyCode != 8) {
                txt.value += seperator;
            }
            return true;
        }
        else {
            return false;
        }
    }

    function ValidateDate(txt, keyCode) {
        if (keyCode == 16)
            isShift = false;
        var val = txt.value;
        var lblmesg = document.getElementById("<%=lblMesg.ClientID%>");
        if (val.length == 10) {
            var splits = val.split("/");
            var dt = new Date(splits[1] + "/" + splits[0] + "/" + splits[2]);

            //Validation for Dates
            if (dt.getDate() == splits[0] && dt.getMonth() + 1 == splits[1]
            && dt.getFullYear() == splits[2]) {
                lblmesg.style.color = "green";
                lblmesg.innerHTML = ""
//                lblmesg.innerHTML = "Valid Date";
            }
            else {
                lblmesg.style.color = "red";
                txt.value = "";
                lblmesg.innerHTML = "Invalid Date";
                return;
            }

            //Range Validation
            if (txt.id.indexOf("txtRange") != -1)
                RangeValidation(dt);

            //BirthDate Validation
            if (txt.id.indexOf("txtBirthDate") != -1)
                BirthDateValidation(dt)
        }
        else if (val.length < 10) {
            lblmesg.style.color = "blue";
            lblmesg.innerHTML = "Required dd/mm/yyyy format.";
        }
    }

</script>
